package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.HealthClock;
import com.example.demo.entity.NocvNews;
import com.example.demo.entity.XueYuan;
import com.example.demo.service.NocvNewsService;
import com.example.demo.service.XueYuanService;
import com.example.demo.vo.DataView;
import com.example.demo.vo.NocvNewsVo;
import com.example.demo.vo.XueYuanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author Coding路人王
 * @date 2022/6/10 8:31
 */
@Controller
public class XueYuanController {
    @Autowired
    private XueYuanService xueYuanService;

    @RequestMapping("/toXueYuan")
    public String toXueYuan(){
        return "xueyuan/xueyuan";
    }


    @RequestMapping("/listXueYuan")
    @ResponseBody
    public DataView listXueYuan(XueYuanVo xueYuanVo) {
        QueryWrapper<XueYuan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(xueYuanVo.getName()),"name",xueYuanVo.getName());
        IPage<XueYuan> iPage = new Page<>(xueYuanVo.getPage(),xueYuanVo.getLimit());
        xueYuanService.page(iPage,queryWrapper);
        return new DataView(iPage.getTotal(),iPage.getRecords());
    }



    /**
     * 新增
     */
    @RequestMapping("/addXueYuan")
    @ResponseBody
    public DataView addXueYuan(XueYuan xueYuan){
        boolean b = xueYuanService.saveOrUpdate(xueYuan);
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功！");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加失败！");
        return dataView;
    }

    @RequestMapping("/updateXueYuan")
    @ResponseBody
    public DataView updateXueYuan(XueYuan xueYuan){
        xueYuanService.updateById(xueYuan);
        DataView dataView = new DataView();
        boolean b = xueYuanService.saveOrUpdate(xueYuan);
        dataView.setCode(200);
        dataView.setMsg("修改成功！");
        return dataView;
    }



    @RequestMapping("/deleteXueYuan")
    @ResponseBody
    public DataView deleteXueYuan(Integer id){
        xueYuanService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除成功！");
        return dataView;
    }





}
